package com.easy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easy.bean.Order;
import com.easy.common.CommonResult;
import com.easy.dao.IOrderDao;
import com.easy.dao.IOrderItemDao;
import com.easy.service.IOrderService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderDao iOrderDao;
    @Autowired
    private IOrderItemDao iOrderItemDao;

    @GetMapping("/all")
    public CommonResult allOrder(@RequestParam(required = false) Integer userId){
        List<Order> orders;

        if(userId == null){
            orders = iOrderDao.selectList(null); // 无条件查询所有订单

        }else{
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",userId)
                    .eq("is_del",0);
            orders = iOrderDao.selectList(wrapper);

        }
        if (orders.isEmpty()) {
            return CommonResult.fail("没有找到订单");
        } else {
            return CommonResult.success(orders);
        }
    }

    @PutMapping("/editpay")
    public CommonResult editPaySum(@RequestParam String orderId,
                             @RequestParam BigDecimal paySum) {
        Order order = iOrderDao.selectById(orderId);
        order.setPaySum(paySum);
        int result = iOrderDao.updateById(order);

        if (result > 0) {
            return CommonResult.success("支付金额修改成功");
        } else {
            return CommonResult.fail("支付金额修改失败");
        }
    }

    @PutMapping("/edit")
    public CommonResult edit(@RequestParam String orderId,
                             @RequestParam(required = false) String addressName,
                             @RequestParam(required = false) String addressPhone,
                             @RequestParam(required = false) String address) {

        // 通过订单ID查询订单
        Order order = iOrderDao.selectById(orderId);
        if (order == null) {
            return CommonResult.fail("订单不存在");
        }

        // 判断每个参数是否为空，如果不为空则更新
        if (addressName != null && !addressName.isEmpty()) {
            order.setAddressName(addressName);
        }

        if (addressPhone != null && !addressPhone.isEmpty()) {
            order.setAddressPhone(addressPhone);
        }

        if (address != null && !address.isEmpty()) {
            order.setAddress(address);
        }

        // 更新订单信息
        int result = iOrderDao.updateById(order);
        if (result > 0) {
            return CommonResult.success("地址更新成功");
        } else {
            return CommonResult.fail("地址更新失败");
        }
    }

    // 用户软删除订单
    @PutMapping("/del")
    public CommonResult edit(@RequestParam String orderId) {
        Order order = iOrderDao.selectById(orderId);
        order.setIsDel(1);
        int result = iOrderDao.updateById(order);
        if (result > 0) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.fail("删除失败");
        }
    }

    // 后台恢复用户删除的订单
    @PutMapping("/return")
    public CommonResult editSums(@RequestParam String orderId) {
        Order order = iOrderDao.selectById(orderId);
        order.setIsDel(0);
        int result = iOrderDao.updateById(order);
        if (result > 0) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.fail("删除失败");
        }
    }





}
